package com.csx.governance.traffic.condition;

import com.csx.governance.traffic.condition.constant.LogicOperator;
import com.csx.governance.traffic.condition.constant.ExpressionComponentType;

import java.util.Collections;
import java.util.List;

/**
 * 表达式逻辑部件
 * @author cuisongxu
 * @date 2023/2/25 15:35
 */
public class LogicComponent implements Component {

    LogicOperator logicOperator;

    public LogicComponent(LogicOperator logicOperator) {
        this.logicOperator = logicOperator;
    }

    @Override
    public String getStatement() {
        return logicOperator.symbol();
    }

    public static class Builder implements Component.Builder {

        @Override
        public Component build(String type, String... params) throws IllegalArgumentException {
            if (ExpressionComponentType.LOGIC.typeName().equals(type) && params.length >= ExpressionComponentType.LOGIC.requiredParamsNumber()) {
                return new LogicComponent(LogicOperator.valueOf(params[0].toUpperCase()));
            } else {
                throw new IllegalArgumentException("Illegal Operation Type");
            }
        }

        @Override
        public List<String> supportedConfigIds() {
            return Collections.singletonList(ExpressionComponentType.LOGIC.typeName());
        }
    }
}
